/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.webapp.common;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Cell;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.Textbox;

/**
 *
 * @author sorasaks
 */
public abstract class DeployCheckboxRenderer extends DeployRowRenderer {

    private static final Logger LOG = Logger.getLogger(DeployCheckboxRenderer.class);
    private Boolean permissionDelete;
    public static final String EXECUTION_CHECKBOX_VALUE = "EXECUTION_CHECKBOX_VALUE";
    public static final String EXECUTION_CHECKBOX_ROW = "EXECUTION_CHECKBOX_ROW";
    protected List<Checkbox> listCheckbox = new ArrayList<Checkbox>();
    private Checkbox chkAll;
    protected Grid grid;
    public List<Object> selectTemplateList = new ArrayList<Object>();
    public List<Checkbox> selectTemplateCheckboxList = new ArrayList<Checkbox>();
    public List<Object> unSelectTemplateList = new ArrayList<Object>();
    public List<Checkbox> unSelectCheckboxList = new ArrayList<Checkbox>();

    protected void refreshRenderer() {
        chkAll.setChecked(false);
    }

    protected Cell createComponentInCell(Component cmpnt, String cellStyle) throws Exception {
        Cell cell = new Cell();
        cell.setSclass(cellStyle);
        cell.appendChild(cmpnt);

        return cell;
    }
    
    protected Cell createComboboxInCell(Combobox combobox, String cellStyle, Boolean isPlace, EventListener event) throws Exception {
        combobox.setInplace(isPlace);

        combobox.addEventListener(Events.ON_CHANGE, event);
        combobox.addEventListener(Events.ON_BLUR, event);

        combobox.setHflex("1");

        Cell cell = new Cell();
        cell.setSclass(cellStyle);
        cell.appendChild(combobox);

        cell.setWidth("98%");

        return cell;
    }

    protected Cell createTextboxInCell(String defaultString, String cellStyle, Boolean isPlace, EventListener event) throws Exception {
        Textbox txtbox = new Textbox();
        txtbox.setValue(defaultString);
        txtbox.setInplace(isPlace);

        txtbox.addEventListener(Events.ON_CHANGE, event);
//        txtbox.addEventListener(Events.ON_BLUR, event);

        txtbox.setHflex("1");

        Cell cell = new Cell();
        cell.setSclass(cellStyle);
        cell.appendChild(txtbox);

        cell.setWidth("98%");

        return cell;
    }

    protected Cell createCheckboxInCell(Object obj, String cellStyle, Row row, int i) throws Exception {
        //Object of row
        Checkbox chk = new Checkbox();
        chk.setAttribute(EXECUTION_CHECKBOX_VALUE, obj);
        chk.setAttribute(EXECUTION_CHECKBOX_ROW, row);
        chk.setValue(i);
        if (permissionDelete != null && permissionDelete == false) {
            chk.setDisabled(true);
        }
        chk.addEventListener(Events.ON_CHECK, new CheckEventListener());

        listCheckbox.add(chk);

        Cell cell = new Cell();
        cell.setSclass(cellStyle);
        cell.appendChild(chk);

        return cell;
    }

    public void setChkAll(Checkbox chkAll) {
        chkAll.addEventListener(Events.ON_CHECK, new CheckboxAllEventListener());
        this.chkAll = chkAll;
    }

    public void setGrid(Grid grid) {
        this.grid = grid;
    }

    private class CheckboxAllEventListener implements EventListener {

        @Override
        public void onEvent(Event t) throws Exception {
            selectTemplateList = new ArrayList<Object>();
            selectTemplateCheckboxList = new ArrayList<Checkbox>();
            unSelectTemplateList = new ArrayList<Object>();
            unSelectCheckboxList = new ArrayList<Checkbox>();

            for (Checkbox chk : listCheckbox) {
                Object obj = (Object) chk.getAttribute(EXECUTION_CHECKBOX_VALUE);
                if (!chk.isDisabled()) {
                    chk.setChecked(chkAll.isChecked());
                    if (chkAll.isChecked()) {
                        selectTemplateList.add(obj);
                        selectTemplateCheckboxList.add(chk);
                    }
                }
            }
        }
    }

    private class CheckEventListener implements EventListener {

        @Override
        public void onEvent(Event t) throws Exception {
            Checkbox chkbox = (Checkbox) t.getTarget();
            Object obj = (Object) chkbox.getAttribute(EXECUTION_CHECKBOX_VALUE);

            if (chkbox.isChecked()) {
                selectTemplateList.add(obj);
                selectTemplateCheckboxList.add(chkbox);
                unSelectTemplateList.remove(obj);
                unSelectCheckboxList.remove(chkbox);
            } else {
                unSelectTemplateList.add(obj);
                unSelectCheckboxList.add(chkbox);
                selectTemplateList.remove(obj);
                selectTemplateCheckboxList.remove(chkbox);
            }

            LOG.info("USER SELECT CHECKBOX RECORD :  ===== " + selectTemplateCheckboxList.size());
            LOG.info("USER SELECT RECORD :  ===== " + selectTemplateList.size());
            LOG.info("USER UNSELECT RECORD :  ===== " + unSelectTemplateList.size());

            if (chkAll != null) {
                Boolean check = Boolean.TRUE;
                for (Checkbox objChk : listCheckbox) {
                    if (!objChk.isDisabled()) {
                        if (!objChk.isChecked()) {
                            check = Boolean.FALSE;
                            break;
                        }
                    }
                }
                chkAll.setChecked(check);
            }
        }
    }

    public Integer getCheckedSizeOfCheckbox() {
        Integer size = 0;

        if (listCheckbox != null && listCheckbox.size() > 0) {
            for (Checkbox chk : listCheckbox) {
                if (chk.isChecked()) {
                    ++size;
                }
            }
        }

        return size;
    }

    public void removeRow(Checkbox chkbox) {
        Rows rows = grid.getRows();
        Row r = (Row) chkbox.getAttribute(EXECUTION_CHECKBOX_ROW);
        rows.removeChild(r);
        row.remove(r);

        chkAll.setChecked(false);
    }
}
